package com.long1008.service;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.long1008.pojo.AllSalary;
import com.long1008.pojo.EmpEc;
import com.long1008.pojo.RespPageEntity;
import com.long1008.pojo.Salary;
import com.long1008.mapper.SalaryMapper;
import com.long1008.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional
public class SalaryService {
    @Value("${mysql.maxInSize}")
    private Integer maxInSize;
    @Autowired
    SalaryMapper salaryMapper;
    public int addSalary(Salary salary) {
        return salaryMapper.addSalary(salary);
    }

    public List<Salary> getAllSalary() {
        return salaryMapper.getAllSalary();
    }

    // 分页
    @Transactional
    public RespPageEntity getAllSalaryByPage(Integer page, Integer size) {
        RespPageEntity pageEntity = new RespPageEntity();
        // 默认从0开始
        if (page != null && size != null) {
            page = (page-1)*size;
        }
        // 获取当前也用户信息
        List<Salary> salarys =  salaryMapper.getAllSalaryByPage(page, size);

        pageEntity.setData(salarys);
        // 获取当前用户总量
        Long totalCount = salaryMapper.getAllSalaryCount();
        pageEntity.setTotal(totalCount);
        return pageEntity;
    }
    public int updateSalary(Salary salary) {
        return salaryMapper.updateSalary(salary);
    }

    public int deleteSalary(String ids) {
        String[] split = ids.split(",");
        return salaryMapper.deleteSalary(split);
    }

    public int updateEmpSalary(Integer sid, Long eid) {
        salaryMapper.deleteSalaryByEid(eid);
        return salaryMapper.addSidAndEid(sid, eid);
    }
    public Salary getEmpSalaryById(Long sid) {
        return salaryMapper.getEmpSalaryById(sid);
    }
    public AllSalary getSalaryByEid(Long eid) {
        return salaryMapper.getSalaryByEid(eid);
    }

    public int getLeaveCountByMonth(Long eid, Date month){
        return salaryMapper.getLeaveCountByMonth(eid, month);
    }
    public EmpEc getLeaveByMonth(Long eid, Date month) {
        return salaryMapper.getLeaveByMonth(eid, month);
    }
    public EmpEc getLateByMonth(Long eid, Date month) {
        return salaryMapper.getLateByMonth(eid, month);
    }
    public EmpEc getOverByMonth(Long eid, Date month) {
        return salaryMapper.getOverByMonth(eid, month);
    }
    public int getLateCountByMonth(Long eid, Date month){
        return salaryMapper.getLateCountByMonth(eid, month);
    }
    public int getLateCount(Long eid){
        return salaryMapper.getLateCount(eid);
    }

    public int getOvertimeByMonth(Long eid, Date month){
        return salaryMapper.getOvertimeByMonth(eid, month);
    }
    public int getOvertime(Long eid){
        return salaryMapper.getOvertime(eid);
    }

    public Salary getSalaryById(Long id){
        return salaryMapper.getSalaryById(id);
    }

    public Integer getLeaveCount(Long eid) {
        return salaryMapper.getLeaveCount(eid);
    }

    public Integer separateBySalary12500() {
        return salaryMapper.separateBySalary12500();
    }

    public Integer separateBySalary15000() {
        return salaryMapper.separateBySalary15000();
    }

    public Integer separateBySalary17500() {
        return salaryMapper.separateBySalary17500();
    }

    public Integer separateBySalary20000() {
        return salaryMapper.separateBySalary20000();
    }

    public Integer separateBySalary22500() {
        return salaryMapper.separateBySalary22500();
    }

    public Integer separateBySalary25000() {
        return salaryMapper.separateBySalary25000();
    }

    public Map<Long, Integer> getSalaryByEids(List<Long> empIdList) {
        Map<Long,Integer> result = new HashMap<>();
        if(CollUtil.isNotEmpty(empIdList)){
            List<AllSalary> resultList = new ArrayList<>(empIdList.size());
            if(empIdList.size() > maxInSize){
                List<List<Long>> partition = Lists.partition(empIdList, maxInSize);
                for (List<Long> ids : partition) {
                    resultList.addAll(salaryMapper.querySalaryByEmpIds(ids));
                }
            }else{
                resultList.addAll(salaryMapper.querySalaryByEmpIds(empIdList));
            }
            for (AllSalary allSalary : resultList) {
                result.put(allSalary.getEid(),allSalary.getMonthSalary());
            }
        }
        return result;
    }

    public Map<String, Map<Long,Integer>> getOvertimeAndLateCountByEids(List<Long> empIdList) {
        Map<String,Map<Long,Integer>> result = new HashMap<>();
        if(CollUtil.isNotEmpty(empIdList)){
            List<EmpEc> resultList = new ArrayList<>(empIdList.size());
            if(empIdList.size() > maxInSize){
                List<List<Long>> partition = Lists.partition(empIdList, maxInSize);
                for (List<Long> ids : partition) {
                    resultList.addAll(salaryMapper.queryEmpEcByEids(ids));
                }
            }else{
                resultList.addAll(salaryMapper.queryEmpEcByEids(empIdList));
            }
            Map<Long, List<EmpEc>> lateMapByEid = resultList.stream()
                    .filter(f -> "迟到".equals(f.getEcReason()))
                    .collect(Collectors.groupingBy(EmpEc::getEid));
            Map<Long,Integer> lateMap = new HashMap<>();
            lateMapByEid.forEach((k,v) -> lateMap.put(k,v.size()));

            Map<Long, List<EmpEc>> overtimeMapByEid = resultList.stream()
                    .filter(f -> "加班".equals(f.getEcReason()))
                    .collect(Collectors.groupingBy(EmpEc::getEid));
            Map<Long,Integer> overtimeMap = new HashMap<>();
            overtimeMapByEid.forEach((k,v) -> overtimeMap.put(k,v.size()));

            Map<Long, List<EmpEc>> leaveMapByEid = resultList.stream()
                    .filter(f -> "请假".equals(f.getEcReason()))
                    .collect(Collectors.groupingBy(EmpEc::getEid));
            Map<Long,Integer> leaveMap = new HashMap<>();
            leaveMapByEid.forEach((k,v) -> leaveMap.put(k,v.size()));
            result.put("late",lateMap);
            result.put("overtime",overtimeMap);
            result.put("leave",leaveMap);
        }
        return result;
    }
    public Map<String, Map<Long, List<EmpEc>>> getOvertimeAndLateAndLeaveByEids(Date month, List<Long> empIdList) {

        Map<String, Map<Long, List<EmpEc>>> result = new HashMap<>();

        if(CollUtil.isNotEmpty(empIdList)){
            List<EmpEc> resultList = new ArrayList<>(empIdList.size());
            if(empIdList.size() > maxInSize){
                List<List<Long>> partition = Lists.partition(empIdList, maxInSize);
                for (List<Long> ids : partition) {
                    resultList.addAll(salaryMapper.queryEmpEcByMonthAndEids(month, ids));
                }
            }else{
                resultList.addAll(salaryMapper.queryEmpEcByMonthAndEids(month, empIdList));
            }
            Map<Long, List<EmpEc>> lateMapByEid = resultList.stream()
                    .filter(f -> "迟到".equals(f.getEcReason()))
                    .collect(Collectors.groupingBy(EmpEc::getEid));
            Map<Long, List<EmpEc>> overtimeMapByEid = resultList.stream()
                    .filter(f -> "加班".equals(f.getEcReason()))
                    .collect(Collectors.groupingBy(EmpEc::getEid));
            Map<Long, List<EmpEc>> leaveMapByEid = resultList.stream()
                    .filter(f -> "请假".equals(f.getEcReason()))
                    .collect(Collectors.groupingBy(EmpEc::getEid));
            result.put("late",lateMapByEid);
            result.put("overtime",overtimeMapByEid);
            result.put("leave",leaveMapByEid);
        }
        return result;
    }
}
